package com.ray.auth.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.auth.system.emun.LinkType;
import com.ray.auth.system.service.ButtonService;
import com.ray.auth.system.service.DictService;
import com.ray.auth.system.service.MenuService;
import com.ray.auth.system.service.RoleService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Dict;
import com.ray.auth.system.table.entity.Role;
import com.ray.auth.system.table.entity.User;
import com.ray.auth.system.vo.Resource;
import com.ray.common.util.DataGridUtil;
import com.ray.common.util.DictLoadUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

/**
 * <h1>角色管理 控制器 </h1>
 * @创建人 ray
 * @创建时间 2017-08-12
 * @版本 1.0
 * @重要修改备注 
 * 
 */
@Controller
@RequestMapping("/system/role")
public class RoleController extends WebController{
	@Autowired
	private RoleService roleService;
	@Autowired
	private ButtonService buttonService;
	@Autowired
	private DictService dictService;
	@Autowired
	private MenuService menuService;
	@Autowired 
	private DictLoadUtil DictLoadUtil;
	/**
	 * 部门页面跳转
	 * @return
	 */
	@RequestMapping(value = "view")
	@RequiresPermissions("role:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus");
	  
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.setViewName("system/role/view");
	  return modelAndView;
	}
	
	/**
	 * 新增页面
	 * @return
	 */
	@RequestMapping(value = "roleAddView")
	@RequiresPermissions("role:add")
	public ModelAndView roleAddView(String menuId,User record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus");
	  
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/role/add");
	  return modelAndView;
	}
	
	/**
	 * 组织新增
	 * @return
	 */
	@RequestMapping(value = "roleAdd")
	@RequiresPermissions("role:add")
	@ResponseBody
	public ResponseEntity<Result<Object>> roleAdd(Role params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = roleService.addRole(params,loginUser);
		return doResult(result);
	}
	
	
	/**
	 * 组织修改页面
	 * @return
	 */
	@RequestMapping(value = "roleUpdateView")
	@RequiresPermissions("role:edit")
	public ModelAndView roleUpdateView(String menuId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  Role record = roleService.queryRole(id,user);
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus");
	  
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/role/update");
	  return modelAndView;
	}
	/**
	 * 组织修改
	 * @return
	 */
	@RequestMapping(value = "roleUpdate")
	@RequiresPermissions("role:edit")
	@ResponseBody
	public ResponseEntity<Result<Object>> roleUpdate(Role params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = roleService.updateRole(params,loginUser);
		return doResult(result);
	}
	
	/**
	 * 组织修改页面
	 * @return
	 */
	@RequestMapping(value = "roleDetailView")
	@RequiresPermissions("role:detail")
	public ModelAndView userDetailView(String menuId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  Role record = roleService.queryRole(id,user);
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus");
	  
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/role/detail");
	  return modelAndView;
	}
	
	/**
	 * 组织删除
	 * @return
	 */
	@RequestMapping(value = "roleDelete")
	@RequiresPermissions("role:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> roleDelete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = roleService.deleteRole(id,loginUser);
		return doResult(result);
	}
	/**
	 * 租户页面跳转
	 * @return
	 */
	@RequestMapping(value = "query")
	@RequiresPermissions("role:query")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> query(Role params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto<Role> page = this.createJQPage(request);
		page = roleService.queryRoleList(params, page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	
	/**
	 * 授权页面
	 * @return
	 */
	@RequestMapping(value = "roleResView")
	@RequiresPermissions("role:resources")
	public ModelAndView roleResView(String menuId,String roleId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("roleId", roleId);
	  modelAndView.setViewName("system/role/resource");
	  return modelAndView;
	}
	
	/**
	 * 授权页面
	 * @return
	 */
	@RequestMapping(value = "roleResChildView")
	@RequiresPermissions("role:resources")
	public ModelAndView roleResChildView(String menuId,String roleId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  List<Resource> list = roleService.queryRoleResource(menuId,roleId,user);
	  modelAndView.addObject("roleId", roleId);
	  modelAndView.addObject("resource", list);
	  modelAndView.setViewName("system/role/resource/info");
	  return modelAndView;
	}
	
	
	/**
	 * 菜单修改
	 * @return
	 */
	@RequestMapping(value = "menu")
	@RequiresPermissions("role:resources")
	@ResponseBody
	public ResponseEntity<Result<Object>> menu(String menuId,String roleId,Boolean check, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = null;
		if(check){
			result = menuService.addMenuRel(menuId,roleId,LinkType.ROLE.getType(),loginUser);
		}else{
			result = menuService.deleteMenuRel(menuId,roleId,LinkType.ROLE.getType(),loginUser);
		}
		
		return doResult(result);
	}
	
	/**
	 * 菜单修改
	 * @return
	 */
	@RequestMapping(value = "button")
	@RequiresPermissions("role:resources")
	@ResponseBody
	public ResponseEntity<Result<Object>> button(String buttonId,String roleId,Boolean check, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = null;
		if(check){
			result = buttonService.addButtonRel(buttonId,roleId,LinkType.ROLE.getType(),loginUser);
		}else{
			result = buttonService.deleteButtonRel(buttonId,roleId,LinkType.ROLE.getType(),loginUser);
		}
		
		return doResult(result);
	}
	
	
}
